package cim3d;

import java.io.File;
import java.io.InputStream;
import java.io.FileOutputStream;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Dong Shufeng
 *         Date: 2009-1-3
 */
public class  Initializor {
    public static boolean isIntilized = false;

    public static void loadLib() {
        if (isIntilized)
            return;
        String[] libs = new String[]{"gluegen-rt.dll", "jogl.dll", "jogl_awt.dll", "jogl_cg.dll"};
        File[] files = new File[libs.length];
        int fileIndex = 0;
        String path = System.getProperty("user.dir");
        for (String lib : libs) {
            try {
                File dll1 = new File(path + File.separator + lib);
                if(dll1.exists()){
                    dll1.delete();
                }
                //Finds a stream to the dll. Change path/class if necessary
                InputStream inputStream = Initializor.class.getResource("/" + lib).openStream();
                //Change name if necessary
                //File temporaryDll = File.createTempFile(lib, ".dll", new File("c:"));
                File temporaryDll = new File(path + File.separator + lib);
                FileOutputStream outputStream = new FileOutputStream(temporaryDll);
                byte[] array = new byte[8192];
                for (int i = inputStream.read(array); i != -1; i = inputStream.read(array)) {
                    outputStream.write(array, 0, i);
                }
                outputStream.close();
                temporaryDll.deleteOnExit();
                files[fileIndex++] = temporaryDll;
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
        isIntilized = true;
    }
}

